<?php

namespace app\common\model\huntermanager;

use think\Model;

use app\common\model\User;
use app\common\model\ads\Order as AdOrder;
use app\common\model\ads\GoldLog;
use app\common\model\MoneyLog;
use app\common\model\printmanager\Order as PrinterOrder;


class Hunter extends Model
{
    

    // 表名
    protected $name = 'self_hunter';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'user_info',
        'gender_text',
        'status_text',
        'display_text'
    ];
    

    
    public function getGenderList()
    {
        return ['0' => __('Gender 0'), '1' => __('Gender 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getDisplayList()
    {
        return ['0' => __('Display 0'), '1' => __('Display 1')];
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDisplayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getUserInfoAttr($value, $data){
        $value = isset($data['user_id']) ? $data['user_id'] : 0;
        return $value ? \app\common\model\User::getBaseInfoNosense($value) : '';
    }

    /**
     * 判断是否为赏金猎人
     */
    public static function isHunter($user_id){
        $where['user_id'] = $user_id;
        $where['status'] = 1;
        $huter_info = self::where($where)->find();
        if(!$huter_info){
            return false;
        }
        return true;
    }


    /**
     * 赏金猎人的下级发布广告奖励
     */
    public static function rewardForAds(AdOrder $order){
        if(!$order->status == 1){
            return false;  
        }
        $rate = config('site.hunter_rate');
        $gold_rate = config('site.rmb_rate');
        
        if(!$rate)  return false;

        $gold_num = bcmul(bcmul($order->real_amount, $rate / 100), $gold_rate, 0);

        if($gold_num <= 0) return false;

        $topUser = User::getTopUser($order->user_id);

        if(!$topUser || !self::isHunter($topUser->id) ){
            return false;
        }

        return User::money($gold_num, $topUser->id, '下级发布广告奖励', $order->user_id, MoneyLog::TYPE_TEAM_AD);
    }


    
    /**
     * 赏金猎人的下级印刷订单奖励
     */
    public static function rewardForPrinter(PrinterOrder $order){
        if(!$order->status == 1){
            return false;  
        }
        $rate = config('site.shop_rate');
        $gold_rate = config('site.rmb_rate');
        
        if(!$rate)  return false;

        $gold_num = bcmul(bcmul($order->real_amount, $rate / 100), $gold_rate, 0);

        if($gold_num <= 0) return false;

        $topUser = User::getTopUser($order->user_id);

        if(!$topUser || !self::isHunter($topUser->id) ){
            return false;
        }

        return User::money($gold_num, $topUser->id, '下级印刷下单奖励', $order->user_id, MoneyLog::TYPE_TEAM_ORDER);
    }

    /**
     * 赏金猎人的下级看广告奖励
     */
    public static function rewardForAdView(GoldLog $goldLog){

        $rate = config('site.hunter_ad_view_rate');
        
        if(!$rate)  return false;

        $gold_num = intval(($goldLog->gold_num * $rate) / 100);

        if($gold_num <= 0) return false;

        $topUser = User::getTopUser($goldLog->user_id);

        if(!$topUser || !self::isHunter($topUser->id) ){
            return false;
        }

        // if(!$topUser){
        //     return User::money($gold_num, $goldLog->user_id, '看广告得赏金分成奖励', $goldLog->user_id, MoneyLog::TYPE_TEAM_GOLD_AD);
        // }

        return User::money($gold_num, $topUser->id, '下级看广告得赏金奖励', $goldLog->user_id, MoneyLog::TYPE_TEAM_GOLD_AD);
    }
}
